/**
 * 
 */
package com.alstudio.module.xmpp;

import android.content.Context;

import com.alstudio.module.BaseModule;
import com.alstudio.module.ModuleEvent;
import com.alstudio.module.ModuleEventType;
import com.alstudio.module.xmpp.EventHandler.network.ALXmppConnetionWrapper;
import com.alstudio.module.xmpp.EventHandler.network.BaseALXmppLoginWrapper;
import com.alstudio.module.xmpp.protocol.xmpp.ALXmppAdapter;
import com.alstudio.module.xmpp.protocol.xmpp.ALXmppException;
import com.alstudio.module.xmpp.request.ALUiXmppRequestEvent;
import com.alstudio.utils.account.ALAccount;
import com.alstudio.utils.android.net.ALConnectionManager;
import com.alstudio.utils.android.net.ALConnectionManager.ALConnectionType;
import com.alstudio.utils.android.net.ALNetworkChangedListener;
import com.alstudio.utils.log.ALLog;

/**
 * 
 * XMPP连接管理器，用于建立XMPP连接、断开XMPP连接、发送XMPP数据。ALXmppConnactionManager监听网络工作状态
 * 
 * @author alonso lee
 * 
 */
public class ALXmppModuleManager extends BaseModule implements
		ALNetworkChangedListener {

	// 缺省XMPP连接管理器
	private static ALXmppModuleManager defaultALXmppConnationManager;

	// 处理登陆
	private BaseALXmppLoginWrapper mXmppLoginWrapper;

	private ALXmppAdapter mXmppAdapter;

	// 本地账号
	private ALAccount localAccount;

	/**
	 * 获取XMPP连接管理器实例
	 * 
	 * @return
	 */
	// public static ALXmppModuleManager getDefaultALXmppConnationManager() {
	// if (defaultALXmppConnationManager == null) {
	// defaultALXmppConnationManager = new ALXmppModuleManager();
	// }
	// return defaultALXmppConnationManager;
	// }

	/**
	 * 
	 */
	public ALXmppModuleManager(ALAccount account, ALXmppAdapter adapter) {
		// TODO Auto-generated constructor stub
		localAccount = account;
		setHandleEvent(ALUiXmppRequestEvent.class);
		mXmppAdapter = adapter;
		mXmppLoginWrapper = new ALXmppConnetionWrapper(localAccount);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.BaseModule#registerModule(android.content.Context)
	 */
	@Override
	public void registerModule(Context context) {
		// TODO Auto-generated method stub
		start();
		// 通知模块管理器模块已经正确开启
		ModuleEvent event = newModuleEvent();
		event.setModuleEventType(ModuleEventType.MODULE_START_SUCCESS);
		postModuleEvent(event);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.BaseModule#unregisterModule(android.content.Context)
	 */
	@Override
	public void unregisterModule(Context context) {
		// TODO Auto-generated method stub
		stop();
		ModuleEvent event = newModuleEvent();
		event.setModuleEventType(ModuleEventType.MODULE_STOP);
		postModuleEvent(event);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.BaseModule#onModuleManagerEvent(java.lang.Object)
	 */
	@Override
	public void onModuleManagerEvent(Object event) {
		// TODO Auto-generated method stub
		ALLog.d("收到模块管理器下发的事件");
		if (mXmppLoginWrapper != null) {
			mXmppLoginWrapper.invokeUIRequest(event);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.module.BaseModule#newModuleEvent()
	 */
	@Override
	public ModuleEvent newModuleEvent() {
		// TODO Auto-generated method stub
		ModuleEvent event = new ModuleEvent(this);
		return event;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.module.BaseModule#setHandleEvent(java.lang.Class)
	 */
	@Override
	public void setHandleEvent(Class<?> event) {
		// TODO Auto-generated method stub
		this.handleEvent = event;
	}

	/**
	 * 开启XMPP连接管理器
	 */
	private void start() {
		ALConnectionManager.AddNetworkChangedListener(this);
		isXmppAdapterValidate(mXmppAdapter);
		mXmppLoginWrapper.registerALXmppAdapter(mXmppAdapter);
	}

	/**
	 * 停止XMPP连接管理器
	 */
	public void stop() {
		releaseALXmppConnationManager();
		if (mXmppLoginWrapper != null) {
			mXmppLoginWrapper.release();
		}
		mXmppLoginWrapper = null;
		// 退出登陆
		if (mXmppAdapter != null) {
			mXmppAdapter.xmppLogOff();
		}
		mXmppAdapter = null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.alstudio.utils.android.net.ALNetworkChangedListener#
	 * onDataConnectionEstablished
	 * (com.alstudio.utils.android.net.ALConnectionManager.ALConnectionType)
	 */
	@Override
	public void onDataConnectionEstablished(ALConnectionType type) {
		// TODO Auto-generated method stub
		ALLog.d("当前可用网络类型为: " + type);
		// 处理网络连接成功事件
		mXmppLoginWrapper.invokeDataConnectionConnected(type);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.utils.android.net.ALNetworkChangedListener#onDataConnectionLost
	 * ()
	 */
	@Override
	public void onDataConnectionLost() {
		// TODO Auto-generated method stub
		ALLog.w("当前无可用网络!");
		mXmppLoginWrapper.invokeDataConnectionLoss();
	}

	/**
	 * 解除监听网络事件
	 */
	private void releaseALXmppConnationManager() {
		if (defaultALXmppConnationManager != null) {
			ALConnectionManager.removeNetworkChangedListener(this);
			defaultALXmppConnationManager = null;
		}
	}

	/**
	 * 检测ALXmppAdapter是否可用
	 */
	protected void isXmppAdapterValidate(ALXmppAdapter adapter) {
		if (adapter == null) {
			throw new ALXmppException("xmpp 中间件未初始化！请首先创建ALXmppAdapter实例");
		}
	}

}
